package com.qujia.station.mqtt;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.qujia.common.constant.Constants;
import com.qujia.common.core.domain.AjaxResult;
import com.qujia.common.core.domain.entity.SysUser;
import com.qujia.common.exception.CustomException;
import com.qujia.common.utils.SecurityUtils;
import com.qujia.station.config.MqttConfig;
import com.qujia.station.constant.ApiConst;
import com.qujia.station.domain.StationDeviceManage;
import com.qujia.station.domain.dto.EquipmentMessageDTO;
import com.qujia.station.domain.dto.MessageDTO;
import com.qujia.station.domain.vo.MessageVO;
import com.qujia.station.mapper.StationDeviceManageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@RestController
public class MqttSenderController {
    @Autowired
    private MqttConfig providerClient;
    @Resource
    private StationDeviceManageMapper stationDeviceManageMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    private static final String OFF = "550c00FF00000000000000000";

    @PostMapping("/sendMessage")
    public AjaxResult sendMessage(@RequestBody MessageDTO message){
        SysUser user = SecurityUtils.getLoginUser().getUser();
        int stationId = user.getStationId().intValue();
        StationDeviceManage stationDeviceManage = stationDeviceManageMapper.selectDeviceNoByStationId(stationId, "9");
        if(null==stationDeviceManage){
            throw new CustomException("该油站暂未接通设备");
        }
        try {
            EquipmentMessageDTO equipmentMessageDTO = BeanUtil.copyProperties(message, EquipmentMessageDTO.class);
            providerClient.publish(stationDeviceManage.getDeviceNo()+ ApiConst.MQTT_PRODUCER_TOPIC,JSON.toJSONString(equipmentMessageDTO));
            //设置开关开启时长
            if(null != message.getDuration()){
                stringRedisTemplate.opsForValue().set(Constants.DEVICE_RELAY_ID+stationDeviceManage.getDeviceNo(), "",message.getDuration(),TimeUnit.MINUTES);
            }
            //关闭设备清除缓存
            if(OFF.equals(message.getData())){
                stringRedisTemplate.delete(Constants.DEVICE_RELAY_ID+stationDeviceManage.getDeviceNo());
            }
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @GetMapping("/selectSwitchStatus")
    public MessageVO selectSwitchStatus(){
        SysUser user = SecurityUtils.getLoginUser().getUser();
        int stationId = user.getStationId().intValue();
        StationDeviceManage stationDeviceManage = stationDeviceManageMapper.selectDeviceNoByStationId(stationId, "9");
        if(null==stationDeviceManage){
            throw new CustomException("该油站暂未接通设备");
        }
        MessageVO messageVO = new MessageVO();
        messageVO.setDuration(stationDeviceManage.getDuration());
        Boolean aBoolean = stringRedisTemplate.hasKey(Constants.DEVICE_RELAY_ID + stationDeviceManage.getDeviceNo());
        if(aBoolean!=null && aBoolean){
            messageVO.setIsOpen(Boolean.TRUE);
        }else {
            messageVO.setIsOpen(Boolean.FALSE);
        }
            return messageVO;
    }
}
